package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.utils.ExceptionMsg;
import com.example.demo.utils.Response;
import com.example.demo.utils.ResponseData;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @author caiyonglan
 * @email 627064518@qq.com
 * @date 2020/12/3 21:32
 */
@RestController
@RequestMapping(value = "/sqltest")
@Api(tags = "MySQL数据库连接测试")
@ApiSupport(author = "627064518@qq.com",order = 2)
public class MysqlConnectController {

    @Autowired
    private UserMapper userMapper;

    @PostMapping("/add")
    @ApiOperation(value = "新增用户", response = Response.class)
    @ApiOperationSupport(ignoreParameters = {"id", "deleted"})
    public Response add(User user){
        userMapper.insert(user);
        return Response.success(ExceptionMsg.INSERTSUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除用户", response = Response.class)
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataTypeClass = Integer.class)
    public Response delete(@PathVariable("id") Integer id){
        userMapper.deleteById(id);
        return Response.success(ExceptionMsg.DELETESUCCESS);
    }


    @GetMapping("/getUserList")
    @ApiOperation(value = "查询所有用户表", response = ResponseData.class)
    public ResponseData getUserList(){
        List<User> users = userMapper.selectList(null);
        return ResponseData.success(ExceptionMsg.SUCCESS, users);
    }

    @GetMapping("/getUser/{id}")
    @ApiOperation(value = "查询单个用户", response = ResponseData.class)
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataTypeClass = Integer.class)
    public ResponseData getUserByUsername(@PathVariable("id") Integer id){
        User user = userMapper.selectById(id);
        if (user != null) {
            return ResponseData.success(ExceptionMsg.SELECTSUCCESS, user);
        } else {
            return ResponseData.error(ExceptionMsg.SELECTFAILED);
        }
    }

    @PatchMapping("/updateUser/{id}")
    @ApiOperation(value = "更新用户", response = ResponseData.class)
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataTypeClass = Integer.class)
    @ApiOperationSupport(ignoreParameters = {"id", "password", "deleted"})
    public Response updateUserById(@PathVariable("id") Integer id, User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        userMapper.update(user, wrapper);
        return Response.success(ExceptionMsg.SUCCESS);
    }

}
